package my.maven.project.service;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import my.maven.project.dao.OrganizationMapper;
import my.maven.project.entity.Organization;

@Service
public class OrganizationService{
 
	@Autowired
	private OrganizationMapper orgMapper;
	
	@Autowired
	OrgServiceNew orgServiceNew;
	
	public List<Organization> getAll(){
		return orgMapper.getAll();
	}

	public Organization getOne(Long id) {
		return orgMapper.getOne(id);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public  void insert(Organization org) {
		orgMapper.insert(org);
	}

	@Transactional
	public void update(Organization org) {
		orgMapper.update(org);
	}

	public void delete(Long id) {
		orgMapper.delete(id);
	}
	
	
	/**
	 * 目标对象的自我调用 Propagation.REQUIRES_NEW 不会起作用的 。
	 * @param org
	 * @param orgId
	 */
	@Transactional(propagation=Propagation.REQUIRED)
	public void testPropagation(Organization org,long orgId)
	{
		insert(org);
		
		Organization entity = getOne(orgId);
		entity.setOrgName("国际神秘组织CIA" + new Random().nextInt(100));
		orgServiceNew.update(entity);
		
		throw new RuntimeException("主动抛出错误，回滚事务");
	}
	
}
